//
//  FindDetailViewController.m
//  TasteEnjoy
//
//  Created by XuRui on 16/5/1.
//  Copyright © 2016年 XuRui. All rights reserved.
//

#import "FindDetailViewController.h"

@interface FindDetailViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, XrLayoutItemHeightDelegate>
@property (nonatomic, retain) BaseCollectionView *collectionView;  /**< collection */
@property (nonatomic, retain) NSMutableArray *arrData;

@property (nonatomic, assign) NSInteger page;/**< 用来保存请求新数据动态值 */

@end

@implementation FindDetailViewController

#pragma mark - ------------ 内存管理 ---------------
- (void)dealloc
{
    _collectionView.delegate = nil;
    _collectionView.dataSource = nil;
    [_collectionView release];
    [_arrData release];
    [_strDetail release];
    [_naviTitle release];
    [super dealloc];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = _naviTitle;
    self.arrData = [NSMutableArray array];
    [self dataHandle];
    
    [self creatCollectionView];
   
    [self refresh];
    
}

#pragma mark - ------------ 数据处理  ---------------

- (void)dataHandle
{
    
    NSString *strID = [_strDetail substringFromIndex:49];
    NSString *str = @"http://203.80.144.212/napi/blog/list/by_category/?cate_key=54589217586df56fb172b4c5&limit=0&locale=zh_CN&screen_height=568&include_fields=sender%2Cfavroite_count%2Calbum%2Cicon_url%2Creply_count%2Clike_count&screen_width=320&start=0&platform_version=9.3.1&app_code=gandalf&platform_name=iPhone%20OS&device_platform=iPhone5%2C2&__domain=www.duitang.com&app_version=6.0.6%20rv%3A156728&device_name=iPhone%205&filter_params=%7B%0A%0A%7D";
    
    
    NSString *url = [str stringByReplacingOccurrencesOfString:@"54589217586df56fb172b4c5" withString:strID];
    NSString *url1 = [url stringByReplacingOccurrencesOfString:@"start=0" withString:[NSString stringWithFormat:@"start=%ld", _page]];
    [XrDataParse GETWithURL:url1 par:nil success:^(id responseObject) {
        
        NSArray *arr = responseObject[@"data"][@"object_list"];
        
        NSArray *arrLoad = [FindDeatilModel modelHandleWithArrar:arr];
        [_arrData addObjectsFromArray:arrLoad];
      
        [_collectionView reloadData];
        
        
    } filed:^(NSError *error) {
        NSLog(@"瀑布流数据加载失败");
    }];
    
    
}

#pragma mark - ------------ 下拉刷新 上拉加载 ---------------

- (void)refresh
{
   //刷新
   self.collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
      _page = 0;
       if (_page == 0) {
           [_arrData removeAllObjects];
       }
       
      [self dataHandle];
      [_collectionView.mj_header endRefreshing];
  }];
    
    //加载
    self.collectionView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
     
        _page += 24;
        [self dataHandle];
        [_collectionView.mj_footer endRefreshing];
    }];
    
}



#pragma mark - ------------ 瀑布流layout---------------
- (void)creatCollectionView
{
    
    // 自定义layout
    XrLayout *layout = [[XrLayout alloc] init];
    //列数
    layout.columCounts = 2;
    
    //列间距
    layout.columSpace = 10;
    //行间距
    layout.rowSpace = 20;
    //边距
    layout.edgeInsets = UIEdgeInsetsMake(10, 10, 10, 10);
    //代理
    layout.delegate = self;
    
    
    //下方collectionView
    self.collectionView = [[BaseCollectionView alloc]initWithFrame:SCREEN_BOUNDS collectionViewLayout:layout];
    //collection代理
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.showsVerticalScrollIndicator = YES;
/** 夜间 */
    _collectionView.dk_backgroundColorPicker = DKColorWithColors(GrayLight,[UIColor grayColor]);
    
    [self.view addSubview:_collectionView];
    [_collectionView registerClass:[FindDetailCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
   
    [layout release];
    [_collectionView release];
    
}

#pragma mark - ------------ 赋值 ---------------
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    FindDetailCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];

    if (_arrData.count != 0) {
        
        cell.backgroundColor = [UIColor whiteColor];
        //cell加个阴影
        cell.layer.shadowOffset = CGSizeMake(0.1, 0.1);
        cell.layer.shadowOpacity = 0.3;
        cell.layer.shadowRadius = 1;
        FindDeatilModel *m = _arrData[indexPath.row];
        cell.labelContent.text = m.msg;
        cell.model = m;
        
 }
    return cell;
}


#pragma mark - ------------ 返回个数 ---------------
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
    return _arrData.count;
    
    
}

#pragma mark - ------------ 设置高度 ---------------
- (CGFloat)layout:(XrLayout *)layout heightForItemAtIndexPath:(NSIndexPath *)indexPath width:(CGFloat)width
{

    FindDeatilModel *m = _arrData[indexPath.item];
    //图片高度
    CGFloat imgH = width * m.height.doubleValue / m.width.doubleValue;
    //文字高度
    CGFloat h1 = [FindDetailCollectionViewCell heightWithText:m.msg];
    //返回行数
    if (h1 >= 15.51 * 6) {
        h1 = 15.51 * 6;
    }

 // 每个cell的高度 = 图片高度 和 文本高度  + 其他控件高度(80)
    return imgH + h1 + 106 ;

}

#pragma mark - ------------ 点击跳转详情 ---------------
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{

    DetailDetailViewController *detail = [[DetailDetailViewController alloc]init];
    FindDeatilModel *m = _arrData[indexPath.item];

    detail.detailID = m.iid.stringValue;
    [self.navigationController pushViewController:detail animated:YES];
    [detail release];

}

#pragma mark - ------------ 将要结束拖拽 当滑动速度>0 导航栏隐藏 <0出现 ---------------
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
{
    
    if (velocity.y > 0) {
        self.navigationController.navigationBar.hidden = YES;
        self.tabBarController.tabBar.hidden = YES;
        
    }else{
        
        self.navigationController.navigationBar.hidden = NO;
        self.tabBarController.tabBar.hidden = NO;

    }
}

#pragma mark - ------------ scrollview 最后走的方法 滑动减速停止 导航栏出现 ---------------
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{

    self.navigationController.navigationBar.hidden = NO;
    self.tabBarController.tabBar.hidden = NO;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
